package com.ljb.android.comm

import android.app.Application
import android.content.Context
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.Utils
import com.didichuxing.doraemonkit.DoraemonKit
import com.ljb.android.comm.common.CommConstant
import com.ljb.android.comm.router.core.RouterManager
import com.ljb.android.comm.router.interceptor.LoginRouterInterceptor
import com.ljb.android.comm.utils.XLog
import com.scwang.smart.refresh.header.FalsifyFooter
import com.scwang.smart.refresh.header.FalsifyHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.smtt.sdk.QbSdk
import net.ljb.kt.HttpConfig

object CommInit {

    //TODO 此处初始化公共组件功能
    fun init(context: Context) {
        initDrawLayout()
        initDokit(context)
        initUtils(context)
        initSP()
        initHttp()
        initWebX5(context)
        initARoute(context)
    }


    private fun initDrawLayout() {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            FalsifyHeader(context)
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> //指定为经典Footer，默认是 BallPulseFooter
            FalsifyFooter(context)
        }
    }

    private fun initDokit(context: Context) {
        if (context is Application) {
            DoraemonKit.install(context, mutableListOf(), BuildConfig.APP_ID);
        }
    }

    private fun initUtils(context: Context) {
        if (context is Application) {
            Utils.init(context)
        }
    }

    private fun initSP() {
        SPStaticUtils.setDefaultSPUtils(
            SPUtils.getInstance(CommConstant.SPKey.SP_NAME)
        )
    }

    private fun initHttp() {
        HttpConfig.Builder(BuildConfig.HTTP_HOST)
            .addCommCookie(object : HttpConfig.ICommCookie {
                //Cookie 持久化
                override fun saveCookie(host: String, cookie: String) {
                    SPStaticUtils.put(host, cookie)
                }

                override fun loadCookie(host: String): String? {
                    return SPStaticUtils.getString(host)
                }
            }).addCommHeader { headers ->
                // 添加公共Header
                headers[CommConstant.Key.KEY_VERSION_APP] = AppUtils.getAppVersionName()
            }.addCommParam { params ->
                //TODO 添加公共Param
            }.openLog(BuildConfig.DEBUG)
            .build()
    }

    private fun initWebX5(context: Context) {
        QbSdk.initX5Environment(context, object : QbSdk.PreInitCallback {

            override fun onCoreInitFinished() {
                XLog.i("WebView X5内核初始化完毕")
            }

            override fun onViewInitFinished(p0: Boolean) {
                XLog.i("X5 WebView验证完毕：$p0")
            }

        })
    }

    private fun initARoute(context: Context) {
        // ARouter 初始化
        if (BuildConfig.DEBUG) {
            ARouter.openDebug()
            ARouter.openLog()
        }
        if (context is Application) {
            ARouter.init(context)
        }

        // 添加自定义拦截器
        RouterManager.addInterceptor(LoginRouterInterceptor(), Int.MIN_VALUE)
    }

}